<?php
/* 
 * name        : Internal » RSS
 * description : Retrieve an RSS feed and make it available to the template.
 * instructions: 
 * 
 */
global $app_root, $site, $smarty;

$default_config = array(
	'feeds' => array(
		'feed' => 'http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/front_page/rss.xml',
		'max'  => 10
	)
);

$config = array_merge($default_config, parse_config($config));

define('MAGPIE_CACHE_DIR', $app_root . $site['home_dir'] . '/cache/plugin_rss');
define('MAGPIE_CACHE_ON', (isset($config['cache_on']) and $config['cache_on']) ? true : false);
define('MAGPIE_CACHE_AGE', isset($config['cache_age']) ? $config['cache_age'] : 3600);
define('MAGPIE_USER_AGENT', 'Webtaculous RSS client');

@mkdir(MAGPIE_CACHE_DIR, 0770, true);

require dirname(__FILE__) . '/magpie/rss_fetch.inc';

foreach ($config['feeds'] as $feed) {
	
	$rss = @fetch_rss($feed['feed'])->items;
	
	if ($rss) {
		$rss = array_slice($rss, 0, isset($feed['max']) ? $feed['max'] : 10);
		
		if (isset($feed['assign'])) {
			$smarty->assign($feed['assign'], $rss);
		} else {
			foreach($rss as $item) {
				echo "<p><a href='{$item['link']}'>{$item['title']}</a><br />{$item['description']}</p>\n";
			}
		}
	}

}
